#data deposit
#T2_sortinocalmar
#compute sortino and calmar
#import fs
sortinocalmar<-data.frame()
for (c in 2:ncol(fs)) {
  #fs[c] <- as.numeric(fs[c])
  sha<-mean(fs[,c],na.rm=T)/sd(fs[,c],na.rm = T)*sqrt(12)
  
  sor<-mean(fs[,c],na.rm=T)/sd(fs[fs[,c] < 0, c],na.rm = T)*sqrt(12)
  
  total<-0
  for (y in unique(substr(fs$month,1,4))) {
    avgcal<-sum(subset(fs,substr(fs$month,1,4)==y)[,c],na.rm = T)/(max(subset(fs,substr(fs$month,1,4)==y)[,c],na.rm = T)-min(subset(fs,substr(fs$month,1,4)==y)[,c],na.rm = T))
    total<-total+avgcal
  }
  cal<-total/26
  
  sortinocalmar<-rbind(sortinocalmar,data.frame(scale=colnames(fs)[c],sharpe=sha, sortino=sor,calmar=cal))
}

#gfc
for (c in 2:ncol(fs)) {
  #fs[c] <- as.numeric(fs[c])
  sha<-mean(fs[133:168,c],na.rm=T)/sd(fs[133:168,c],na.rm = T)*sqrt(12)
  
  sor<-mean(fs[133:168,c],na.rm=T)/sd(fs[fs[133:168,c] < 0, c],na.rm = T)*sqrt(12)
  
  total<-0
  for (y in unique(substr(fs[133:168,]$month,1,4))) {
    avgcal<-sum(subset(fs,substr(fs$month,1,4)==y)[,c],na.rm = T)/(max(subset(fs,substr(fs$month,1,4)==y)[,c],na.rm = T)-min(subset(fs,substr(fs$month,1,4)==y)[,c],na.rm = T))
    total<-total+avgcal
  }
  cal<-total/3
  
  sortinocalmar<-rbind(sortinocalmar,data.frame(scale=colnames(fs)[c],sharpe=sha, sortino=sor,calmar=cal))
}




#T3_performance measures for leverate-sorted portfolios
#import portscaled
sortinocalmar_lev_idio<-data.frame()
for (q in c("ql","q2","q3","qh")) {
  lev<-subset(portscaled_firmlevel,portfolio==paste("leverage",q,sep = "_"))
  for (c in 3:ncol(lev)) {
    #lev[c] <- as.numeric(lev[c])
    sha<-mean(lev[,c],na.rm=T)/sd(lev[,c],na.rm = T)*sqrt(12)
    
    sor<-mean(lev[,c],na.rm=T)/sd(lev[lev[,c] < 0, c],na.rm = T)*sqrt(12)
    
    total<-0
    for (y in unique(substr(lev$month,1,4))) {
      avgcal<-sum(subset(lev,substr(lev$month,1,4)==y)[,c],na.rm = T)/(max(subset(lev,substr(lev$month,1,4)==y)[,c],na.rm = T)-min(subset(lev,substr(lev$month,1,4)==y)[,c],na.rm = T))
      total<-total+avgcal
    }
    cal<-total/26
    
    sortinocalmar_lev_idio<-rbind(sortinocalmar_lev_idio,data.frame(portfolio=paste("leverage",q,sep = "_"),scale=colnames(lev)[c],sharpe=sha,sortino=sor,calmar=cal))
  }
}

#gfc
for (q in c("ql","q2","q3","qh")) {
  lev<-subset(portscaled_firmlevel,portfolio==paste("leverage",q,sep = "_"))
  for (c in 3:ncol(lev)) {
    #lev[c] <- as.numeric(lev[c])
    sha<-mean(lev[133:168,c],na.rm=T)/sd(lev[133:168,c],na.rm = T)*sqrt(12)
    
    sor<-mean(lev[133:168,c],na.rm=T)/sd(lev[lev[133:168,c] < 0, c],na.rm = T)*sqrt(12)
    
    total<-0
    for (y in unique(substr(lev[133:168,]$month,1,4))) {
      avgcal<-sum(subset(lev,substr(lev$month,1,4)==y)[,c],na.rm = T)/(max(subset(lev,substr(lev$month,1,4)==y)[,c],na.rm = T)-min(subset(lev,substr(lev$month,1,4)==y)[,c],na.rm = T))
      total<-total+avgcal
    }
    cal<-total/3
    
    sortinocalmar_lev_idio<-rbind(sortinocalmar_lev_idio,data.frame(portfolio=paste("leverage",q,sep = "_"),scale=colnames(lev)[c],sharpe=sha,sortino=sor,calmar=cal))
  }
}




#T4_spanning regressions for full sample
library(plm)
library(lmtest)
library(sandwich)

jan_result<-data.frame()
model <- plm(rvmgd ~ xret + jan, data = firmlevelscaled_csts, model = "within")
jan_result<-rbind(jan_result,data.frame(scaling="rvmgd",alpha=within_intercept(model)[1]*12,
                                        stderr_alpha=attr(within_intercept(model), "se")*12,
                                        beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                        adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                        t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                        p_b=summary(model)$coefficients[1,4]))

model <- plm(ivmgd ~ xret + jan, data = firmlevelscaled_csts, model = "within")
jan_result<-rbind(jan_result,data.frame(scaling="ivmgd",alpha=within_intercept(model)[1]*12,
                                        stderr_alpha=attr(within_intercept(model), "se")*12,
                                        beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                        adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                        t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                        p_b=summary(model)$coefficients[1,4]))

model <- plm(mwmgd ~ xret + jan, data = firmlevelscaled_csts, model = "within")
jan_result<-rbind(jan_result,data.frame(scaling="mwmgd",alpha=within_intercept(model)[1]*12,
                                        stderr_alpha=attr(within_intercept(model), "se")*12,
                                        beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                        adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                        t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                        p_b=summary(model)$coefficients[1,4]))

model <- plm(glbmgd ~ xret + jan, data = firmlevelscaled_csts, model = "within")
jan_result<-rbind(jan_result,data.frame(scaling="glbmgd",alpha=within_intercept(model)[1]*12,
                                        stderr_alpha=attr(within_intercept(model), "se")*12,
                                        beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                        adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                        t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                        p_b=summary(model)$coefficients[1,4]))



model <- plm(rvmgd ~ xret + XR+SMB+HML+RMW+CMA+UMD+ jan, data = firmlevelscaled_csts, model = "within")
jan_result<-rbind(jan_result,data.frame(scaling="rvmgd_ctrl",alpha=within_intercept(model)[1]*12,
                                        stderr_alpha=attr(within_intercept(model), "se")*12,
                                        beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                        adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                        t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                        p_b=summary(model)$coefficients[1,4]))

model <- plm(ivmgd ~ xret +XR+SMB+HML+RMW+CMA+UMD+ jan, data = firmlevelscaled_csts, model = "within")
jan_result<-rbind(jan_result,data.frame(scaling="ivmgd_ctrl",alpha=within_intercept(model)[1]*12,
                                        stderr_alpha=attr(within_intercept(model), "se")*12,
                                        beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                        adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                        t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                        p_b=summary(model)$coefficients[1,4]))

model <- plm(mwmgd ~ xret +XR+SMB+HML+RMW+CMA+UMD+ jan, data = firmlevelscaled_csts, model = "within")
jan_result<-rbind(jan_result,data.frame(scaling="mwmgd_ctrl",alpha=within_intercept(model)[1]*12,
                                        stderr_alpha=attr(within_intercept(model), "se")*12,
                                        beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                        adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                        t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                        p_b=summary(model)$coefficients[1,4]))

model <- plm(glbmgd ~ xret +XR+SMB+HML+RMW+CMA+UMD+ jan, data = firmlevelscaled_csts, model = "within")
jan_result<-rbind(jan_result,data.frame(scaling="glbmgd_ctrl",alpha=within_intercept(model)[1]*12,
                                        stderr_alpha=attr(within_intercept(model), "se")*12,
                                        beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                        adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                        t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                        p_b=summary(model)$coefficients[1,4]))






model <- plm(rvmgd ~ xret + jan, data = subset(firmlevelscaled_csts,month>200706&month<200907), model = "within")
jan_result<-rbind(jan_result,data.frame(scaling="rvmgd_gfc",alpha=within_intercept(model)[1]*12,
                                        stderr_alpha=attr(within_intercept(model), "se")*12,
                                        beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                        adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                        t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                        p_b=summary(model)$coefficients[1,4]))

model <- plm(ivmgd ~ xret + jan, data = subset(firmlevelscaled_csts,month>200706&month<200907), model = "within")
jan_result<-rbind(jan_result,data.frame(scaling="ivmgd_gfc",alpha=within_intercept(model)[1]*12,
                                        stderr_alpha=attr(within_intercept(model), "se")*12,
                                        beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                        adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                        t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                        p_b=summary(model)$coefficients[1,4]))

model <- plm(mwmgd ~ xret + jan, data = subset(firmlevelscaled_csts,month>200706&month<200907), model = "within")
jan_result<-rbind(jan_result,data.frame(scaling="mwmgd_gfc",alpha=within_intercept(model)[1]*12,
                                        stderr_alpha=attr(within_intercept(model), "se")*12,
                                        beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                        adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                        t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                        p_b=summary(model)$coefficients[1,4]))

model <- plm(glbmgd ~ xret + jan, data = subset(firmlevelscaled_csts,month>200706&month<200907), model = "within")
jan_result<-rbind(jan_result,data.frame(scaling="glbmgd_gfc",alpha=within_intercept(model)[1]*12,
                                        stderr_alpha=attr(within_intercept(model), "se")*12,
                                        beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                        adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                        t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                        p_b=summary(model)$coefficients[1,4]))



model <- plm(rvmgd ~ xret + XR+SMB+HML+RMW+CMA+UMD+ jan, data = subset(firmlevelscaled_csts,month>200706&month<200907), model = "within")
jan_result<-rbind(jan_result,data.frame(scaling="rvmgd_ctrl_gfc",alpha=within_intercept(model)[1]*12,
                                        stderr_alpha=attr(within_intercept(model), "se")*12,
                                        beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                        adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                        t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                        p_b=summary(model)$coefficients[1,4]))

model <- plm(ivmgd ~ xret +XR+SMB+HML+RMW+CMA+UMD+ jan, data = subset(firmlevelscaled_csts,month>200706&month<200907), model = "within")
jan_result<-rbind(jan_result,data.frame(scaling="ivmgd_ctrl_gfc",alpha=within_intercept(model)[1]*12,
                                        stderr_alpha=attr(within_intercept(model), "se")*12,
                                        beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                        adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                        t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                        p_b=summary(model)$coefficients[1,4]))

model <- plm(mwmgd ~ xret +XR+SMB+HML+RMW+CMA+UMD+ jan, data = subset(firmlevelscaled_csts,month>200706&month<200907), model = "within")
jan_result<-rbind(jan_result,data.frame(scaling="mwmgd_ctrl_gfc",alpha=within_intercept(model)[1]*12,
                                        stderr_alpha=attr(within_intercept(model), "se")*12,
                                        beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                        adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                        t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                        p_b=summary(model)$coefficients[1,4]))

model <- plm(glbmgd ~ xret +XR+SMB+HML+RMW+CMA+UMD+ jan, data = subset(firmlevelscaled_csts,month>200706&month<200907), model = "within")
jan_result<-rbind(jan_result,data.frame(scaling="glbmgd_ctrl_gfc",alpha=within_intercept(model)[1]*12,
                                        stderr_alpha=attr(within_intercept(model), "se")*12,
                                        beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                        adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                        t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                        p_b=summary(model)$coefficients[1,4]))





#T6_bab
colnames(babreg)[13]<-"MktRF"
colnames(babreg)[14]<-"SMB"
colnames(babreg)[15]<-"HML"
colnames(babreg)[16]<-"Mom"
colnames(babreg)[17]<-"fsskew"
babregresult<-data.frame()
modelfs<-lm(bab~MktRF+SMB+HML,data=babreg)
babregresult<-rbind(babregresult,data.frame(regression="fs",alpha=summary(modelfs)$coefficients[1,1]*12,stderr_alpha=summary(modelfs)$coefficients[1,2],
                                            pvalue_alpha=summary(modelfs)$coefficients[1,4],beta=summary(modelfs)$coefficients[2,1],
                                            stderr_beta=summary(modelfs)$coefficients[2,2],pvalue_beta=summary(modelfs)$coefficients[2,4],
                                            adjrsq=summary(modelfs)$adj.r.squared,ar=summary(modelfs)$coefficients[1,1]/summary(modelfs)$sigma*sqrt(12)))
rm(modelfs)
modelfs_ctrl<-lm(bab~MktRF+SMB+HML+Mom+fsskew,data=babreg)
babregresult<-rbind(babregresult,data.frame(regression="fs_ctrl",alpha=summary(modelfs_ctrl)$coefficients[1,1]*12,stderr_alpha=summary(modelfs_ctrl)$coefficients[1,2],
                                            pvalue_alpha=summary(modelfs_ctrl)$coefficients[1,4],beta=summary(modelfs_ctrl)$coefficients[2,1],
                                            stderr_beta=summary(modelfs_ctrl)$coefficients[2,2],pvalue_beta=summary(modelfs_ctrl)$coefficients[2,4],
                                            adjrsq=summary(modelfs_ctrl)$adj.r.squared,ar=summary(modelfs_ctrl)$coefficients[1,1]/summary(modelfs_ctrl)$sigma*sqrt(12)))
rm(modelfs_ctrl)

modelpre<-lm(bab~MktRF+SMB+HML,data=babreg[1:287,])
babregresult<-rbind(babregresult,data.frame(regression="pre",alpha=summary(modelpre)$coefficients[1,1]*12,stderr_alpha=summary(modelpre)$coefficients[1,2],
                                            pvalue_alpha=summary(modelpre)$coefficients[1,4],beta=summary(modelpre)$coefficients[2,1],
                                            stderr_beta=summary(modelpre)$coefficients[2,2],pvalue_beta=summary(modelpre)$coefficients[2,4],
                                            adjrsq=summary(modelpre)$adj.r.squared,ar=summary(modelpre)$coefficients[1,1]/summary(modelpre)$sigma*sqrt(12)))
rm(modelpre)
modelpre_ctrl<-lm(bab~MktRF+SMB+HML+Mom,data=babreg[1:287,])
babregresult<-rbind(babregresult,data.frame(regression="pre_ctrl",alpha=summary(modelpre_ctrl)$coefficients[1,1]*12,stderr_alpha=summary(modelpre_ctrl)$coefficients[1,2],
                                            pvalue_alpha=summary(modelpre_ctrl)$coefficients[1,4],beta=summary(modelpre_ctrl)$coefficients[2,1],
                                            stderr_beta=summary(modelpre_ctrl)$coefficients[2,2],pvalue_beta=summary(modelpre_ctrl)$coefficients[2,4],
                                            adjrsq=summary(modelpre_ctrl)$adj.r.squared,ar=summary(modelpre_ctrl)$coefficients[1,1]/summary(modelpre_ctrl)$sigma*sqrt(12)))
rm(modelpre_ctrl)

modelpost<-lm(bab~MktRF+SMB+HML,data=babreg[288:311,])
babregresult<-rbind(babregresult,data.frame(regression="post",alpha=summary(modelpost)$coefficients[1,1]*12,stderr_alpha=summary(modelpost)$coefficients[1,2],
                                            pvalue_alpha=summary(modelpost)$coefficients[1,4],beta=summary(modelpost)$coefficients[2,1],
                                            stderr_beta=summary(modelpost)$coefficients[2,2],pvalue_beta=summary(modelpost)$coefficients[2,4],
                                            adjrsq=summary(modelpost)$adj.r.squared,ar=summary(modelpost)$coefficients[1,1]/summary(modelpost)$sigma*sqrt(12)))
rm(modelpost)
modelpost_ctrl<-lm(bab~MktRF+SMB+HML+Mom,data=babreg[288:311,])
babregresult<-rbind(babregresult,data.frame(regression="post_ctrl",alpha=summary(modelpost_ctrl)$coefficients[1,1]*12,stderr_alpha=summary(modelpost_ctrl)$coefficients[1,2],
                                            pvalue_alpha=summary(modelpost_ctrl)$coefficients[1,4],beta=summary(modelpost_ctrl)$coefficients[2,1],
                                            stderr_beta=summary(modelpost_ctrl)$coefficients[2,2],pvalue_beta=summary(modelpost_ctrl)$coefficients[2,4],
                                            adjrsq=summary(modelpost_ctrl)$adj.r.squared,ar=summary(modelpost_ctrl)$coefficients[1,1]/summary(modelpost_ctrl)$sigma*sqrt(12)))
rm(modelpost_ctrl)

for (i in 1:10) {
  modelfs<-lm(babreg[,i+2]~babreg$MktRF+babreg$SMB+babreg$HML)
  babregresult<-rbind(babregresult,data.frame(regression=colnames(babreg)[i+2],alpha=summary(modelfs)$coefficients[1,1]*12,stderr_alpha=summary(modelfs)$coefficients[1,2],
                                              pvalue_alpha=summary(modelfs)$coefficients[1,4],beta=summary(modelfs)$coefficients[2,1],
                                              stderr_beta=summary(modelfs)$coefficients[2,2],pvalue_beta=summary(modelfs)$coefficients[2,4],
                                              adjrsq=summary(modelfs)$adj.r.squared,ar=summary(modelfs)$coefficients[1,1]/summary(modelfs)$sigma*sqrt(12)))
  rm(modelfs)
  modelfs_ctrl<-lm(babreg[,i+2]~babreg$MktRF+babreg$SMB+babreg$HML+babreg$Mom+babreg$fsskew)
  babregresult<-rbind(babregresult,data.frame(regression=paste0(colnames(babreg)[i+2],"_ctrl",sep=""),alpha=summary(modelfs_ctrl)$coefficients[1,1]*12,stderr_alpha=summary(modelfs_ctrl)$coefficients[1,2],
                                              pvalue_alpha=summary(modelfs_ctrl)$coefficients[1,4],beta=summary(modelfs_ctrl)$coefficients[2,1],
                                              stderr_beta=summary(modelfs_ctrl)$coefficients[2,2],pvalue_beta=summary(modelfs_ctrl)$coefficients[2,4],
                                              adjrsq=summary(modelfs_ctrl)$adj.r.squared,ar=summary(modelfs_ctrl)$coefficients[1,1]/summary(modelfs_ctrl)$sigma*sqrt(12)))
  rm(modelfs_ctrl)
  
  modelpre<-lm(babreg[1:287,i+2]~babreg$MktRF[1:287]+babreg$SMB[1:287]+babreg$HML[1:287])
  babregresult<-rbind(babregresult,data.frame(regression=paste0(colnames(babreg)[i+2],"pre",sep=""),alpha=summary(modelpre)$coefficients[1,1]*12,stderr_alpha=summary(modelpre)$coefficients[1,2],
                                              pvalue_alpha=summary(modelpre)$coefficients[1,4],beta=summary(modelpre)$coefficients[2,1],
                                              stderr_beta=summary(modelpre)$coefficients[2,2],pvalue_beta=summary(modelpre)$coefficients[2,4],
                                              adjrsq=summary(modelpre)$adj.r.squared,ar=summary(modelpre)$coefficients[1,1]/summary(modelpre)$sigma*sqrt(12)))
  rm(modelpre)
  modelpre_ctrl<-lm(babreg[1:287,i+2]~babreg$MktRF[1:287]+babreg$SMB[1:287]+babreg$HML[1:287]+babreg$Mom[1:287])
  babregresult<-rbind(babregresult,data.frame(regression=paste0(colnames(babreg)[i+2],"pre_ctrl",sep=""),alpha=summary(modelpre_ctrl)$coefficients[1,1]*12,stderr_alpha=summary(modelpre_ctrl)$coefficients[1,2],
                                              pvalue_alpha=summary(modelpre_ctrl)$coefficients[1,4],beta=summary(modelpre_ctrl)$coefficients[2,1],
                                              stderr_beta=summary(modelpre_ctrl)$coefficients[2,2],pvalue_beta=summary(modelpre_ctrl)$coefficients[2,4],
                                              adjrsq=summary(modelpre_ctrl)$adj.r.squared,ar=summary(modelpre_ctrl)$coefficients[1,1]/summary(modelpre_ctrl)$sigma*sqrt(12)))
  rm(modelpre_ctrl)
  
  modelpost<-lm(babreg[288:311,i+2]~babreg$MktRF[288:311]+babreg$SMB[288:311]+babreg$HML[288:311])
  babregresult<-rbind(babregresult,data.frame(regression=paste0(colnames(babreg)[i+2],"post",sep=""),alpha=summary(modelpost)$coefficients[1,1]*12,stderr_alpha=summary(modelpost)$coefficients[1,2],
                                              pvalue_alpha=summary(modelpost)$coefficients[1,4],beta=summary(modelpost)$coefficients[2,1],
                                              stderr_beta=summary(modelpost)$coefficients[2,2],pvalue_beta=summary(modelpost)$coefficients[2,4],
                                              adjrsq=summary(modelpost)$adj.r.squared,ar=summary(modelpost)$coefficients[1,1]/summary(modelpost)$sigma*sqrt(12)))
  rm(modelpost)
  modelpost_ctrl<-lm(babreg[288:311,i+2]~babreg$MktRF[288:311]+babreg$SMB[288:311]+babreg$HML[288:311]+babreg$Mom[288:311])
  babregresult<-rbind(babregresult,data.frame(regression=paste0(colnames(babreg)[i+2],"post_ctrl",sep=""),alpha=summary(modelpost_ctrl)$coefficients[1,1]*12,stderr_alpha=summary(modelpost_ctrl)$coefficients[1,2],
                                              pvalue_alpha=summary(modelpost_ctrl)$coefficients[1,4],beta=summary(modelpost_ctrl)$coefficients[2,1],
                                              stderr_beta=summary(modelpost_ctrl)$coefficients[2,2],pvalue_beta=summary(modelpost_ctrl)$coefficients[2,4],
                                              adjrsq=summary(modelpost_ctrl)$adj.r.squared,ar=summary(modelpost_ctrl)$coefficients[1,1]/summary(modelpost_ctrl)$sigma*sqrt(12)))
  rm(modelpost_ctrl)
}





#T8_double sorting
sort_levsorcal_result<-data.frame()
for (q in 1:4) {
  ss<-subset(sort_levsorcal_ts,quartile==q)
  
  shaxret<-mean(ss$xret_sha,na.rm=T)/sd(ss$xret_sha,na.rm = T)*sqrt(12)
  sharv<-mean(ss$rv_sha,na.rm=T)/sd(ss$rv_sha,na.rm = T)*sqrt(12)
  shaiv<-mean(ss$iv_sha,na.rm=T)/sd(ss$iv_sha,na.rm = T)*sqrt(12)
  shamw<-mean(ss$mw_sha,na.rm=T)/sd(ss$mw_sha,na.rm = T)*sqrt(12)
  shaglb<-mean(ss$glb_sha,na.rm=T)/sd(ss$glb_sha,na.rm = T)*sqrt(12)
  
  sorxret<-mean(ss$xret_sor,na.rm=T)/sd(ss[ss$xret_sor < 0,]$xret_sor,na.rm = T)*sqrt(12)
  sorrv<-mean(ss$rv_sor,na.rm=T)/sd(ss[ss$rv_sor < 0,]$rv_sor,na.rm = T)*sqrt(12)
  soriv<-mean(ss$iv_sor,na.rm=T)/sd(ss[ss$iv_sor < 0,]$iv_sor,na.rm = T)*sqrt(12)
  sormw<-mean(ss$mw_sor,na.rm=T)/sd(ss[ss$mw_sor < 0,]$mw_sor,na.rm = T)*sqrt(12)
  sorglb<-mean(ss$glb_sor,na.rm=T)/sd(ss[ss$glb_sor < 0,]$glb_sor,na.rm = T)*sqrt(12)
  
  total<-0
  for (y in unique(substr(ss$month,1,4))) {
    avgcal<-sum(subset(ss,substr(ss$month,1,4)==y)$xret_cal,na.rm = T)/(max(subset(ss,substr(ss$month,1,4)==y)$xret_cal,na.rm = T)-min(subset(ss,substr(ss$month,1,4)==y)$xret_cal,na.rm = T))
    total<-total+avgcal
  }
  calxret<-total/26
  total<-0
  for (y in unique(substr(ss$month,1,4))) {
    avgcal<-sum(subset(ss,substr(ss$month,1,4)==y)$rv_cal,na.rm = T)/(max(subset(ss,substr(ss$month,1,4)==y)$rv_cal,na.rm = T)-min(subset(ss,substr(ss$month,1,4)==y)$rv_cal,na.rm = T))
    total<-total+avgcal
  }
  calrv<-total/26
  total<-0
  for (y in unique(substr(ss$month,1,4))) {
    avgcal<-sum(subset(ss,substr(ss$month,1,4)==y)$iv_cal,na.rm = T)/(max(subset(ss,substr(ss$month,1,4)==y)$iv_cal,na.rm = T)-min(subset(ss,substr(ss$month,1,4)==y)$iv_cal,na.rm = T))
    total<-total+avgcal
  }
  caliv<-total/26
  total<-0
  for (y in unique(substr(ss$month,1,4))) {
    avgcal<-sum(subset(ss,substr(ss$month,1,4)==y)$mw_cal,na.rm = T)/(max(subset(ss,substr(ss$month,1,4)==y)$mw_cal,na.rm = T)-min(subset(ss,substr(ss$month,1,4)==y)$mw_cal,na.rm = T))
    total<-total+avgcal
  }
  calmw<-total/26
  total<-0
  for (y in unique(substr(ss$month,1,4))) {
    avgcal<-sum(subset(ss,substr(ss$month,1,4)==y)$glb_cal,na.rm = T)/(max(subset(ss,substr(ss$month,1,4)==y)$glb_cal,na.rm = T)-min(subset(ss,substr(ss$month,1,4)==y)$glb_cal,na.rm = T))
    total<-total+avgcal
  }
  calglb<-total/26
  
  sort_levsorcal_result<-rbind(sort_levsorcal_result,data.frame(quartile=q,shaxret=shaxret,sharv=sharv,shaiv=shaiv,shamw=shamw,shaglb=shaglb,
                                                                sorxret=sorxret,sorrv=sorrv,soriv=soriv,sormw=sormw,sorglb=sorglb,
                                                                calxret=calxret,calrv=calrv,caliv=caliv,calmw=calmw,calglb=calglb))
}





#T11_longshort_panel
library(plm)
library(lmtest)
library(sandwich)
firmlevelscaled_cstsls <- pdata.frame(firmlevelscaled_cstsls, index = c("firm","trend"))

csts_result<-data.frame()
model <- plm(rvmgd ~ rvmgd_agt + rvmgd_ls_vw + jan, data = firmlevelscaled_cstsls, model = "within")
csts_result<-rbind(csts_result,data.frame(scaling="rvmgd_vw",alpha=within_intercept(model)[1]*12,
                                          stderr_alpha=attr(within_intercept(model), "se")*12,
                                          beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                          beta2=summary(model)$coefficients[2,1],stderr_beta2=summary(model)$coefficients[2,2],
                                          adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                          t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                          p_b=summary(model)$coefficients[1,4],
                                          p_b2=summary(model)$coefficients[2,4]))

model <- plm(ivmgd ~ ivmgd_agt + ivmgd_ls_vw + jan, data = firmlevelscaled_cstsls, model = "within")
csts_result<-rbind(csts_result,data.frame(scaling="ivmgd_vw",alpha=within_intercept(model)[1]*12,
                                          stderr_alpha=attr(within_intercept(model), "se")*12,
                                          beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                          beta2=summary(model)$coefficients[2,1],stderr_beta2=summary(model)$coefficients[2,2],
                                          adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                          t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                          p_b=summary(model)$coefficients[1,4],
                                          p_b2=summary(model)$coefficients[2,4]))

model <- plm(mwmgd ~ mwmgd_agt + mwmgd_ls_vw + jan, data = firmlevelscaled_cstsls, model = "within")
csts_result<-rbind(csts_result,data.frame(scaling="mwmgd_vw",alpha=within_intercept(model)[1]*12,
                                          stderr_alpha=attr(within_intercept(model), "se")*12,
                                          beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                          beta2=summary(model)$coefficients[2,1],stderr_beta2=summary(model)$coefficients[2,2],
                                          adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                          t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                          p_b=summary(model)$coefficients[1,4],
                                          p_b2=summary(model)$coefficients[2,4]))

model <- plm(glbmgd ~ glbmgd_agt + glbmgd_ls_vw + jan, data = firmlevelscaled_cstsls, model = "within")
csts_result<-rbind(csts_result,data.frame(scaling="glbmgd_vw",alpha=within_intercept(model)[1]*12,
                                          stderr_alpha=attr(within_intercept(model), "se")*12,
                                          beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                          beta2=summary(model)$coefficients[2,1],stderr_beta2=summary(model)$coefficients[2,2],
                                          adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                          t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                          p_b=summary(model)$coefficients[1,4],
                                          p_b2=summary(model)$coefficients[2,4]))



model <- plm(rvmgd ~ rvmgd_agt + rvmgd_ls_vw + XR+SMB+HML+RMW+CMA+UMD + jan, data = firmlevelscaled_cstsls, model = "within")
csts_result<-rbind(csts_result,data.frame(scaling="rvmgd_vw_ctrl",alpha=within_intercept(model)[1]*12,
                                          stderr_alpha=attr(within_intercept(model), "se")*12,
                                          beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                          beta2=summary(model)$coefficients[2,1],stderr_beta2=summary(model)$coefficients[2,2],
                                          adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                          t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                          p_b=summary(model)$coefficients[1,4],
                                          p_b2=summary(model)$coefficients[2,4]))

model <- plm(ivmgd ~ ivmgd_agt + ivmgd_ls_vw + XR+SMB+HML+RMW+CMA+UMD + jan, data = firmlevelscaled_cstsls, model = "within")
csts_result<-rbind(csts_result,data.frame(scaling="ivmgd_vw_ctrl",alpha=within_intercept(model)[1]*12,
                                          stderr_alpha=attr(within_intercept(model), "se")*12,
                                          beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                          beta2=summary(model)$coefficients[2,1],stderr_beta2=summary(model)$coefficients[2,2],
                                          adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                          t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                          p_b=summary(model)$coefficients[1,4],
                                          p_b2=summary(model)$coefficients[2,4]))

model <- plm(mwmgd ~ mwmgd_agt + mwmgd_ls_vw + XR+SMB+HML+RMW+CMA+UMD + jan, data = firmlevelscaled_cstsls, model = "within")
csts_result<-rbind(csts_result,data.frame(scaling="mwmgd_vw_ctrl",alpha=within_intercept(model)[1]*12,
                                          stderr_alpha=attr(within_intercept(model), "se")*12,
                                          beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                          beta2=summary(model)$coefficients[2,1],stderr_beta2=summary(model)$coefficients[2,2],
                                          adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                          t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                          p_b=summary(model)$coefficients[1,4],
                                          p_b2=summary(model)$coefficients[2,4]))

model <- plm(glbmgd ~ glbmgd_agt + glbmgd_ls_vw + XR+SMB+HML+RMW+CMA+UMD + jan, data = firmlevelscaled_cstsls, model = "within")
csts_result<-rbind(csts_result,data.frame(scaling="glbmgd_vw_ctrl",alpha=within_intercept(model)[1]*12,
                                          stderr_alpha=attr(within_intercept(model), "se")*12,
                                          beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                          beta2=summary(model)$coefficients[2,1],stderr_beta2=summary(model)$coefficients[2,2],
                                          adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                          t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                          p_b=summary(model)$coefficients[1,4],
                                          p_b2=summary(model)$coefficients[2,4]))




model <- plm(rvmgd ~ rvmgd_agt + rvmgd_ls_vw + jan, data = subset(firmlevelscaled_cstsls,month>200706&month<200907), model = "within")
csts_result<-rbind(csts_result,data.frame(scaling="rvmgd_vw_gfc",alpha=within_intercept(model)[1]*12,
                                          stderr_alpha=attr(within_intercept(model), "se")*12,
                                          beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                          beta2=summary(model)$coefficients[2,1],stderr_beta2=summary(model)$coefficients[2,2],
                                          adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                          t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                          p_b=summary(model)$coefficients[1,4],
                                          p_b2=summary(model)$coefficients[2,4]))

model <- plm(ivmgd ~ ivmgd_agt + ivmgd_ls_vw + jan, data = subset(firmlevelscaled_cstsls,month>200706&month<200907), model = "within")
csts_result<-rbind(csts_result,data.frame(scaling="ivmgd_vw_gfc",alpha=within_intercept(model)[1]*12,
                                          stderr_alpha=attr(within_intercept(model), "se")*12,
                                          beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                          beta2=summary(model)$coefficients[2,1],stderr_beta2=summary(model)$coefficients[2,2],
                                          adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                          t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                          p_b=summary(model)$coefficients[1,4],
                                          p_b2=summary(model)$coefficients[2,4]))

model <- plm(mwmgd ~ mwmgd_agt + mwmgd_ls_vw + jan, data = subset(firmlevelscaled_cstsls,month>200706&month<200907), model = "within")
csts_result<-rbind(csts_result,data.frame(scaling="mwmgd_vw_gfc",alpha=within_intercept(model)[1]*12,
                                          stderr_alpha=attr(within_intercept(model), "se")*12,
                                          beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                          beta2=summary(model)$coefficients[2,1],stderr_beta2=summary(model)$coefficients[2,2],
                                          adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                          t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                          p_b=summary(model)$coefficients[1,4],
                                          p_b2=summary(model)$coefficients[2,4]))

model <- plm(glbmgd ~ glbmgd_agt + glbmgd_ls_vw + jan, data = subset(firmlevelscaled_cstsls,month>200706&month<200907), model = "within")
csts_result<-rbind(csts_result,data.frame(scaling="glbmgd_vw_gfc",alpha=within_intercept(model)[1]*12,
                                          stderr_alpha=attr(within_intercept(model), "se")*12,
                                          beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                          beta2=summary(model)$coefficients[2,1],stderr_beta2=summary(model)$coefficients[2,2],
                                          adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                          t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                          p_b=summary(model)$coefficients[1,4],
                                          p_b2=summary(model)$coefficients[2,4]))



model <- plm(rvmgd ~ rvmgd_agt + rvmgd_ls_vw + XR+SMB+HML+RMW+CMA+UMD + jan, data = subset(firmlevelscaled_cstsls,month>200706&month<200907), model = "within")
csts_result<-rbind(csts_result,data.frame(scaling="rvmgd_vw_ctrl_gfc",alpha=within_intercept(model)[1]*12,
                                          stderr_alpha=attr(within_intercept(model), "se")*12,
                                          beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                          beta2=summary(model)$coefficients[2,1],stderr_beta2=summary(model)$coefficients[2,2],
                                          adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                          t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                          p_b=summary(model)$coefficients[1,4],
                                          p_b2=summary(model)$coefficients[2,4]))

model <- plm(ivmgd ~ ivmgd_agt + ivmgd_ls_vw + XR+SMB+HML+RMW+CMA+UMD + jan, data = subset(firmlevelscaled_cstsls,month>200706&month<200907), model = "within")
csts_result<-rbind(csts_result,data.frame(scaling="ivmgd_vw_ctrl_gfc",alpha=within_intercept(model)[1]*12,
                                          stderr_alpha=attr(within_intercept(model), "se")*12,
                                          beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                          beta2=summary(model)$coefficients[2,1],stderr_beta2=summary(model)$coefficients[2,2],
                                          adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                          t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                          p_b=summary(model)$coefficients[1,4],
                                          p_b2=summary(model)$coefficients[2,4]))

model <- plm(mwmgd ~ mwmgd_agt + mwmgd_ls_vw + XR+SMB+HML+RMW+CMA+UMD + jan, data = subset(firmlevelscaled_cstsls,month>200706&month<200907), model = "within")
csts_result<-rbind(csts_result,data.frame(scaling="mwmgd_vw_ctrl_gfc",alpha=within_intercept(model)[1]*12,
                                          stderr_alpha=attr(within_intercept(model), "se")*12,
                                          beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                          beta2=summary(model)$coefficients[2,1],stderr_beta2=summary(model)$coefficients[2,2],
                                          adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                          t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                          p_b=summary(model)$coefficients[1,4],
                                          p_b2=summary(model)$coefficients[2,4]))

model <- plm(glbmgd ~ glbmgd_agt + glbmgd_ls_vw + XR+SMB+HML+RMW+CMA+UMD + jan, data = subset(firmlevelscaled_cstsls,month>200706&month<200907), model = "within")
csts_result<-rbind(csts_result,data.frame(scaling="glbmgd_vw_ctrl_gfc",alpha=within_intercept(model)[1]*12,
                                          stderr_alpha=attr(within_intercept(model), "se")*12,
                                          beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                          beta2=summary(model)$coefficients[2,1],stderr_beta2=summary(model)$coefficients[2,2],
                                          adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                          t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                          p_b=summary(model)$coefficients[1,4],
                                          p_b2=summary(model)$coefficients[2,4]))




#T12_longshort_ts
csts_result<-data.frame()
model <- lm(firmrvmgd ~ aggregateavgrvmgd + lsrv_vw + jan, data = fs)
csts_result<-rbind(csts_result,data.frame(scaling="rvmgd_vw",alpha=summary(model)$coefficients[1,1]*12,stderr_alpha=summary(model)$coefficients[1,2]*12,
                                          p_a=summary(model)$coefficients[1,4],
                                          beta1=summary(model)$coefficients[2,1],stderr_beta=summary(model)$coefficients[2,2],
                                          p_b1=summary(model)$coefficients[2,4],
                                          beta2=summary(model)$coefficients[3,1],stderr_beta=summary(model)$coefficients[3,2],
                                          p_b2=summary(model)$coefficients[3,4],
                                          adjrsq=summary(model)$adj.r.squared,ar=summary(model)$coefficients[1,1]/summary(model)$sigma*sqrt(12)))

model <- lm(firmivmgd ~ aggregateivmgd + lsiv_vw + jan, data = fs)
csts_result<-rbind(csts_result,data.frame(scaling="ivmgd_vw",alpha=summary(model)$coefficients[1,1]*12,stderr_alpha=summary(model)$coefficients[1,2]*12,
                                          p_a=summary(model)$coefficients[1,4],
                                          beta1=summary(model)$coefficients[2,1],stderr_beta=summary(model)$coefficients[2,2],
                                          p_b1=summary(model)$coefficients[2,4],
                                          beta2=summary(model)$coefficients[3,1],stderr_beta=summary(model)$coefficients[3,2],
                                          p_b2=summary(model)$coefficients[3,4],
                                          adjrsq=summary(model)$adj.r.squared,ar=summary(model)$coefficients[1,1]/summary(model)$sigma*sqrt(12)))

model <- lm(firmmwmgd ~ aggregatemwmgd + lsmw_vw + jan, data = fs)
csts_result<-rbind(csts_result,data.frame(scaling="mwmgd_vw",alpha=summary(model)$coefficients[1,1]*12,stderr_alpha=summary(model)$coefficients[1,2]*12,
                                          p_a=summary(model)$coefficients[1,4],
                                          beta1=summary(model)$coefficients[2,1],stderr_beta=summary(model)$coefficients[2,2],
                                          p_b1=summary(model)$coefficients[2,4],
                                          beta2=summary(model)$coefficients[3,1],stderr_beta=summary(model)$coefficients[3,2],
                                          p_b2=summary(model)$coefficients[3,4],
                                          adjrsq=summary(model)$adj.r.squared,ar=summary(model)$coefficients[1,1]/summary(model)$sigma*sqrt(12)))

model <- lm(firmglbmgd ~ aggregateglbmgd + lsglb_vw + jan, data = fs)
csts_result<-rbind(csts_result,data.frame(scaling="glbmgd_vw",alpha=summary(model)$coefficients[1,1]*12,stderr_alpha=summary(model)$coefficients[1,2]*12,
                                          p_a=summary(model)$coefficients[1,4],
                                          beta1=summary(model)$coefficients[2,1],stderr_beta=summary(model)$coefficients[2,2],
                                          p_b1=summary(model)$coefficients[2,4],
                                          beta2=summary(model)$coefficients[3,1],stderr_beta=summary(model)$coefficients[3,2],
                                          p_b2=summary(model)$coefficients[3,4],
                                          adjrsq=summary(model)$adj.r.squared,ar=summary(model)$coefficients[1,1]/summary(model)$sigma*sqrt(12)))

model <- lm(firmrvmgd ~ aggregateavgrvmgd + lsrv_vw + XR+SMB+HML+RMW+CMA+UMD + jan, data = fs)
csts_result<-rbind(csts_result,data.frame(scaling="rvmgd_ctrl_vw",alpha=summary(model)$coefficients[1,1]*12,stderr_alpha=summary(model)$coefficients[1,2]*12,
                                          p_a=summary(model)$coefficients[1,4],
                                          beta1=summary(model)$coefficients[2,1],stderr_beta=summary(model)$coefficients[2,2],
                                          p_b1=summary(model)$coefficients[2,4],
                                          beta2=summary(model)$coefficients[3,1],stderr_beta=summary(model)$coefficients[3,2],
                                          p_b2=summary(model)$coefficients[3,4],
                                          adjrsq=summary(model)$adj.r.squared,ar=summary(model)$coefficients[1,1]/summary(model)$sigma*sqrt(12)))

model <- lm(firmivmgd ~ aggregateivmgd + lsiv_vw + XR+SMB+HML+RMW+CMA+UMD + jan, data = fs)
csts_result<-rbind(csts_result,data.frame(scaling="ivmgd_ctrl_vw",alpha=summary(model)$coefficients[1,1]*12,stderr_alpha=summary(model)$coefficients[1,2]*12,
                                          p_a=summary(model)$coefficients[1,4],
                                          beta1=summary(model)$coefficients[2,1],stderr_beta=summary(model)$coefficients[2,2],
                                          p_b1=summary(model)$coefficients[2,4],
                                          beta2=summary(model)$coefficients[3,1],stderr_beta=summary(model)$coefficients[3,2],
                                          p_b2=summary(model)$coefficients[3,4],
                                          adjrsq=summary(model)$adj.r.squared,ar=summary(model)$coefficients[1,1]/summary(model)$sigma*sqrt(12)))

model <- lm(firmmwmgd ~ aggregatemwmgd + lsmw_vw + XR+SMB+HML+RMW+CMA+UMD + jan, data = fs)
csts_result<-rbind(csts_result,data.frame(scaling="mwmgd_ctrl_vw",alpha=summary(model)$coefficients[1,1]*12,stderr_alpha=summary(model)$coefficients[1,2]*12,
                                          p_a=summary(model)$coefficients[1,4],
                                          beta1=summary(model)$coefficients[2,1],stderr_beta=summary(model)$coefficients[2,2],
                                          p_b1=summary(model)$coefficients[2,4],
                                          beta2=summary(model)$coefficients[3,1],stderr_beta=summary(model)$coefficients[3,2],
                                          p_b2=summary(model)$coefficients[3,4],
                                          adjrsq=summary(model)$adj.r.squared,ar=summary(model)$coefficients[1,1]/summary(model)$sigma*sqrt(12)))

model <- lm(firmglbmgd ~ aggregateglbmgd + lsglb_vw + XR+SMB+HML+RMW+CMA+UMD + jan, data = fs)
csts_result<-rbind(csts_result,data.frame(scaling="glbmgd_ctrl_vw",alpha=summary(model)$coefficients[1,1]*12,stderr_alpha=summary(model)$coefficients[1,2]*12,
                                          p_a=summary(model)$coefficients[1,4],
                                          beta1=summary(model)$coefficients[2,1],stderr_beta=summary(model)$coefficients[2,2],
                                          p_b1=summary(model)$coefficients[2,4],
                                          beta2=summary(model)$coefficients[3,1],stderr_beta=summary(model)$coefficients[3,2],
                                          p_b2=summary(model)$coefficients[3,4],
                                          adjrsq=summary(model)$adj.r.squared,ar=summary(model)$coefficients[1,1]/summary(model)$sigma*sqrt(12)))




model <- lm(firmrvmgd ~ aggregateavgrvmgd + lsrv_vw + jan, data = subset(fs,month>200706&month<200907))
csts_result<-rbind(csts_result,data.frame(scaling="rvmgd_gfc_vw",alpha=summary(model)$coefficients[1,1]*12,stderr_alpha=summary(model)$coefficients[1,2]*12,
                                          p_a=summary(model)$coefficients[1,4],
                                          beta1=summary(model)$coefficients[2,1],stderr_beta=summary(model)$coefficients[2,2],
                                          p_b1=summary(model)$coefficients[2,4],
                                          beta2=summary(model)$coefficients[3,1],stderr_beta=summary(model)$coefficients[3,2],
                                          p_b2=summary(model)$coefficients[3,4],
                                          adjrsq=summary(model)$adj.r.squared,ar=summary(model)$coefficients[1,1]/summary(model)$sigma*sqrt(12)))

model <- lm(firmivmgd ~ aggregateivmgd + lsiv_vw + jan, data = subset(fs,month>200706&month<200907))
csts_result<-rbind(csts_result,data.frame(scaling="ivmgd_gfc_vw",alpha=summary(model)$coefficients[1,1]*12,stderr_alpha=summary(model)$coefficients[1,2]*12,
                                          p_a=summary(model)$coefficients[1,4],
                                          beta1=summary(model)$coefficients[2,1],stderr_beta=summary(model)$coefficients[2,2],
                                          p_b1=summary(model)$coefficients[2,4],
                                          beta2=summary(model)$coefficients[3,1],stderr_beta=summary(model)$coefficients[3,2],
                                          p_b2=summary(model)$coefficients[3,4],
                                          adjrsq=summary(model)$adj.r.squared,ar=summary(model)$coefficients[1,1]/summary(model)$sigma*sqrt(12)))

model <- lm(firmmwmgd ~ aggregatemwmgd + lsmw_vw + jan, data = subset(fs,month>200706&month<200907))
csts_result<-rbind(csts_result,data.frame(scaling="mwmgd_gfc_vw",alpha=summary(model)$coefficients[1,1]*12,stderr_alpha=summary(model)$coefficients[1,2]*12,
                                          p_a=summary(model)$coefficients[1,4],
                                          beta1=summary(model)$coefficients[2,1],stderr_beta=summary(model)$coefficients[2,2],
                                          p_b1=summary(model)$coefficients[2,4],
                                          beta2=summary(model)$coefficients[3,1],stderr_beta=summary(model)$coefficients[3,2],
                                          p_b2=summary(model)$coefficients[3,4],
                                          adjrsq=summary(model)$adj.r.squared,ar=summary(model)$coefficients[1,1]/summary(model)$sigma*sqrt(12)))

model <- lm(firmglbmgd ~ aggregateglbmgd + lsglb_vw + jan, data = subset(fs,month>200706&month<200907))
csts_result<-rbind(csts_result,data.frame(scaling="glbmgd_gfc_vw",alpha=summary(model)$coefficients[1,1]*12,stderr_alpha=summary(model)$coefficients[1,2]*12,
                                          p_a=summary(model)$coefficients[1,4],
                                          beta1=summary(model)$coefficients[2,1],stderr_beta=summary(model)$coefficients[2,2],
                                          p_b1=summary(model)$coefficients[2,4],
                                          beta2=summary(model)$coefficients[3,1],stderr_beta=summary(model)$coefficients[3,2],
                                          p_b2=summary(model)$coefficients[3,4],
                                          adjrsq=summary(model)$adj.r.squared,ar=summary(model)$coefficients[1,1]/summary(model)$sigma*sqrt(12)))

model <- lm(firmrvmgd ~ aggregateavgrvmgd + lsrv_vw + XR+SMB+HML+RMW+CMA+UMD + jan, data = subset(fs,month>200706&month<200907))
csts_result<-rbind(csts_result,data.frame(scaling="rvmgd_ctrl_gfc_vw",alpha=summary(model)$coefficients[1,1]*12,stderr_alpha=summary(model)$coefficients[1,2]*12,
                                          p_a=summary(model)$coefficients[1,4],
                                          beta1=summary(model)$coefficients[2,1],stderr_beta=summary(model)$coefficients[2,2],
                                          p_b1=summary(model)$coefficients[2,4],
                                          beta2=summary(model)$coefficients[3,1],stderr_beta=summary(model)$coefficients[3,2],
                                          p_b2=summary(model)$coefficients[3,4],
                                          adjrsq=summary(model)$adj.r.squared,ar=summary(model)$coefficients[1,1]/summary(model)$sigma*sqrt(12)))

model <- lm(firmivmgd ~ aggregateivmgd + lsiv_vw + XR+SMB+HML+RMW+CMA+UMD + jan, data = subset(fs,month>200706&month<200907))
csts_result<-rbind(csts_result,data.frame(scaling="ivmgd_ctrl_gfc_vw",alpha=summary(model)$coefficients[1,1]*12,stderr_alpha=summary(model)$coefficients[1,2]*12,
                                          p_a=summary(model)$coefficients[1,4],
                                          beta1=summary(model)$coefficients[2,1],stderr_beta=summary(model)$coefficients[2,2],
                                          p_b1=summary(model)$coefficients[2,4],
                                          beta2=summary(model)$coefficients[3,1],stderr_beta=summary(model)$coefficients[3,2],
                                          p_b2=summary(model)$coefficients[3,4],
                                          adjrsq=summary(model)$adj.r.squared,ar=summary(model)$coefficients[1,1]/summary(model)$sigma*sqrt(12)))

model <- lm(firmmwmgd ~ aggregatemwmgd + lsmw_vw + XR+SMB+HML+RMW+CMA+UMD + jan, data = subset(fs,month>200706&month<200907))
csts_result<-rbind(csts_result,data.frame(scaling="mwmgd_ctrl_gfc_vw",alpha=summary(model)$coefficients[1,1]*12,stderr_alpha=summary(model)$coefficients[1,2]*12,
                                          p_a=summary(model)$coefficients[1,4],
                                          beta1=summary(model)$coefficients[2,1],stderr_beta=summary(model)$coefficients[2,2],
                                          p_b1=summary(model)$coefficients[2,4],
                                          beta2=summary(model)$coefficients[3,1],stderr_beta=summary(model)$coefficients[3,2],
                                          p_b2=summary(model)$coefficients[3,4],
                                          adjrsq=summary(model)$adj.r.squared,ar=summary(model)$coefficients[1,1]/summary(model)$sigma*sqrt(12)))

model <- lm(firmglbmgd ~ aggregateglbmgd + lsglb_vw + XR+SMB+HML+RMW+CMA+UMD + jan, data = subset(fs,month>200706&month<200907))
csts_result<-rbind(csts_result,data.frame(scaling="glbmgd_ctrl_gfc_vw",alpha=summary(model)$coefficients[1,1]*12,stderr_alpha=summary(model)$coefficients[1,2]*12,
                                          p_a=summary(model)$coefficients[1,4],
                                          beta1=summary(model)$coefficients[2,1],stderr_beta=summary(model)$coefficients[2,2],
                                          p_b1=summary(model)$coefficients[2,4],
                                          beta2=summary(model)$coefficients[3,1],stderr_beta=summary(model)$coefficients[3,2],
                                          p_b2=summary(model)$coefficients[3,4],
                                          adjrsq=summary(model)$adj.r.squared,ar=summary(model)$coefficients[1,1]/summary(model)$sigma*sqrt(12)))




#T13_longshortx_panel
csts_result<-data.frame()
model <- plm(rvmgd ~ rvmgd_agt + rvmgd_ls_vw + jan, data = firmlevelscaled_cstsls, model = "within")
csts_result<-rbind(csts_result,data.frame(scaling="rvmgd_vw",alpha=within_intercept(model)[1]*12,
                                          stderr_alpha=attr(within_intercept(model), "se")*12,
                                          beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                          beta2=summary(model)$coefficients[2,1],stderr_beta2=summary(model)$coefficients[2,2],
                                          adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                          t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                          p_b=summary(model)$coefficients[1,4],
                                          p_b2=summary(model)$coefficients[2,4]))

model <- plm(ivmgd ~ ivmgd_agt + ivmgd_ls_vw + jan, data = firmlevelscaled_cstsls, model = "within")
csts_result<-rbind(csts_result,data.frame(scaling="ivmgd_vw",alpha=within_intercept(model)[1]*12,
                                          stderr_alpha=attr(within_intercept(model), "se")*12,
                                          beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                          beta2=summary(model)$coefficients[2,1],stderr_beta2=summary(model)$coefficients[2,2],
                                          adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                          t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                          p_b=summary(model)$coefficients[1,4],
                                          p_b2=summary(model)$coefficients[2,4]))

model <- plm(mwmgd ~ mwmgd_agt + mwmgd_ls_vw + jan, data = firmlevelscaled_cstsls, model = "within")
csts_result<-rbind(csts_result,data.frame(scaling="mwmgd_vw",alpha=within_intercept(model)[1]*12,
                                          stderr_alpha=attr(within_intercept(model), "se")*12,
                                          beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                          beta2=summary(model)$coefficients[2,1],stderr_beta2=summary(model)$coefficients[2,2],
                                          adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                          t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                          p_b=summary(model)$coefficients[1,4],
                                          p_b2=summary(model)$coefficients[2,4]))

model <- plm(glbmgd ~ glbmgd_agt + glbmgd_ls_vw + jan, data = firmlevelscaled_cstsls, model = "within")
csts_result<-rbind(csts_result,data.frame(scaling="glbmgd_vw",alpha=within_intercept(model)[1]*12,
                                          stderr_alpha=attr(within_intercept(model), "se")*12,
                                          beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                          beta2=summary(model)$coefficients[2,1],stderr_beta2=summary(model)$coefficients[2,2],
                                          adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                          t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                          p_b=summary(model)$coefficients[1,4],
                                          p_b2=summary(model)$coefficients[2,4]))



model <- plm(rvmgd ~ rvmgd_agt + rvmgd_ls_vw + XR+SMB+HML+RMW+CMA+UMD + jan, data = firmlevelscaled_cstsls, model = "within")
csts_result<-rbind(csts_result,data.frame(scaling="rvmgd_vw_ctrl",alpha=within_intercept(model)[1]*12,
                                          stderr_alpha=attr(within_intercept(model), "se")*12,
                                          beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                          beta2=summary(model)$coefficients[2,1],stderr_beta2=summary(model)$coefficients[2,2],
                                          adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                          t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                          p_b=summary(model)$coefficients[1,4],
                                          p_b2=summary(model)$coefficients[2,4]))

model <- plm(ivmgd ~ ivmgd_agt + ivmgd_ls_vw + XR+SMB+HML+RMW+CMA+UMD + jan, data = firmlevelscaled_cstsls, model = "within")
csts_result<-rbind(csts_result,data.frame(scaling="ivmgd_vw_ctrl",alpha=within_intercept(model)[1]*12,
                                          stderr_alpha=attr(within_intercept(model), "se")*12,
                                          beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                          beta2=summary(model)$coefficients[2,1],stderr_beta2=summary(model)$coefficients[2,2],
                                          adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                          t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                          p_b=summary(model)$coefficients[1,4],
                                          p_b2=summary(model)$coefficients[2,4]))

model <- plm(mwmgd ~ mwmgd_agt + mwmgd_ls_vw + XR+SMB+HML+RMW+CMA+UMD + jan, data = firmlevelscaled_cstsls, model = "within")
csts_result<-rbind(csts_result,data.frame(scaling="mwmgd_vw_ctrl",alpha=within_intercept(model)[1]*12,
                                          stderr_alpha=attr(within_intercept(model), "se")*12,
                                          beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                          beta2=summary(model)$coefficients[2,1],stderr_beta2=summary(model)$coefficients[2,2],
                                          adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                          t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                          p_b=summary(model)$coefficients[1,4],
                                          p_b2=summary(model)$coefficients[2,4]))

model <- plm(glbmgd ~ glbmgd_agt + glbmgd_ls_vw + XR+SMB+HML+RMW+CMA+UMD + jan, data = firmlevelscaled_cstsls, model = "within")
csts_result<-rbind(csts_result,data.frame(scaling="glbmgd_vw_ctrl",alpha=within_intercept(model)[1]*12,
                                          stderr_alpha=attr(within_intercept(model), "se")*12,
                                          beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                          beta2=summary(model)$coefficients[2,1],stderr_beta2=summary(model)$coefficients[2,2],
                                          adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                          t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                          p_b=summary(model)$coefficients[1,4],
                                          p_b2=summary(model)$coefficients[2,4]))




model <- plm(rvmgd ~ rvmgd_agt + rvmgd_ls_vw + jan, data = subset(firmlevelscaled_cstsls,month>200706&month<200907), model = "within")
csts_result<-rbind(csts_result,data.frame(scaling="rvmgd_vw_gfc",alpha=within_intercept(model)[1]*12,
                                          stderr_alpha=attr(within_intercept(model), "se")*12,
                                          beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                          beta2=summary(model)$coefficients[2,1],stderr_beta2=summary(model)$coefficients[2,2],
                                          adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                          t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                          p_b=summary(model)$coefficients[1,4],
                                          p_b2=summary(model)$coefficients[2,4]))

model <- plm(ivmgd ~ ivmgd_agt + ivmgd_ls_vw + jan, data = subset(firmlevelscaled_cstsls,month>200706&month<200907), model = "within")
csts_result<-rbind(csts_result,data.frame(scaling="ivmgd_vw_gfc",alpha=within_intercept(model)[1]*12,
                                          stderr_alpha=attr(within_intercept(model), "se")*12,
                                          beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                          beta2=summary(model)$coefficients[2,1],stderr_beta2=summary(model)$coefficients[2,2],
                                          adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                          t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                          p_b=summary(model)$coefficients[1,4],
                                          p_b2=summary(model)$coefficients[2,4]))

model <- plm(mwmgd ~ mwmgd_agt + mwmgd_ls_vw + jan, data = subset(firmlevelscaled_cstsls,month>200706&month<200907), model = "within")
csts_result<-rbind(csts_result,data.frame(scaling="mwmgd_vw_gfc",alpha=within_intercept(model)[1]*12,
                                          stderr_alpha=attr(within_intercept(model), "se")*12,
                                          beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                          beta2=summary(model)$coefficients[2,1],stderr_beta2=summary(model)$coefficients[2,2],
                                          adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                          t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                          p_b=summary(model)$coefficients[1,4],
                                          p_b2=summary(model)$coefficients[2,4]))

model <- plm(glbmgd ~ glbmgd_agt + glbmgd_ls_vw + jan, data = subset(firmlevelscaled_cstsls,month>200706&month<200907), model = "within")
csts_result<-rbind(csts_result,data.frame(scaling="glbmgd_vw_gfc",alpha=within_intercept(model)[1]*12,
                                          stderr_alpha=attr(within_intercept(model), "se")*12,
                                          beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                          beta2=summary(model)$coefficients[2,1],stderr_beta2=summary(model)$coefficients[2,2],
                                          adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                          t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                          p_b=summary(model)$coefficients[1,4],
                                          p_b2=summary(model)$coefficients[2,4]))



model <- plm(rvmgd ~ rvmgd_agt + rvmgd_ls_vw + XR+SMB+HML+RMW+CMA+UMD + jan, data = subset(firmlevelscaled_cstsls,month>200706&month<200907), model = "within")
csts_result<-rbind(csts_result,data.frame(scaling="rvmgd_vw_ctrl_gfc",alpha=within_intercept(model)[1]*12,
                                          stderr_alpha=attr(within_intercept(model), "se")*12,
                                          beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                          beta2=summary(model)$coefficients[2,1],stderr_beta2=summary(model)$coefficients[2,2],
                                          adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                          t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                          p_b=summary(model)$coefficients[1,4],
                                          p_b2=summary(model)$coefficients[2,4]))

model <- plm(ivmgd ~ ivmgd_agt + ivmgd_ls_vw + XR+SMB+HML+RMW+CMA+UMD + jan, data = subset(firmlevelscaled_cstsls,month>200706&month<200907), model = "within")
csts_result<-rbind(csts_result,data.frame(scaling="ivmgd_vw_ctrl_gfc",alpha=within_intercept(model)[1]*12,
                                          stderr_alpha=attr(within_intercept(model), "se")*12,
                                          beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                          beta2=summary(model)$coefficients[2,1],stderr_beta2=summary(model)$coefficients[2,2],
                                          adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                          t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                          p_b=summary(model)$coefficients[1,4],
                                          p_b2=summary(model)$coefficients[2,4]))

model <- plm(mwmgd ~ mwmgd_agt + mwmgd_ls_vw + XR+SMB+HML+RMW+CMA+UMD + jan, data = subset(firmlevelscaled_cstsls,month>200706&month<200907), model = "within")
csts_result<-rbind(csts_result,data.frame(scaling="mwmgd_vw_ctrl_gfc",alpha=within_intercept(model)[1]*12,
                                          stderr_alpha=attr(within_intercept(model), "se")*12,
                                          beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                          beta2=summary(model)$coefficients[2,1],stderr_beta2=summary(model)$coefficients[2,2],
                                          adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                          t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                          p_b=summary(model)$coefficients[1,4],
                                          p_b2=summary(model)$coefficients[2,4]))

model <- plm(glbmgd ~ glbmgd_agt + glbmgd_ls_vw + XR+SMB+HML+RMW+CMA+UMD + jan, data = subset(firmlevelscaled_cstsls,month>200706&month<200907), model = "within")
csts_result<-rbind(csts_result,data.frame(scaling="glbmgd_vw_ctrl_gfc",alpha=within_intercept(model)[1]*12,
                                          stderr_alpha=attr(within_intercept(model), "se")*12,
                                          beta=summary(model)$coefficients[1,1],stderr_beta=summary(model)$coefficients[1,2],
                                          beta2=summary(model)$coefficients[2,1],stderr_beta2=summary(model)$coefficients[2,2],
                                          adjrsq=r.squared(model, dfcor = TRUE),ar=within_intercept(model)[1]/sigma(model)*sqrt(12),
                                          t_a=within_intercept(model)[1]/attr(within_intercept(model), "se"),
                                          p_b=summary(model)$coefficients[1,4],
                                          p_b2=summary(model)$coefficients[2,4]))

rm(model)






#T14_longshortx_ts
csts_result<-data.frame()
model <- lm(firmrvmgd ~ aggregateavgrvmgd + lsrv_vw + agt_x_rvlsvw + jan, data = fs)
csts_result<-rbind(csts_result,data.frame(scaling="rvmgd_vw",alpha=summary(model)$coefficients[1,1]*12,stderr_alpha=summary(model)$coefficients[1,2]*12,
                                          p_a=summary(model)$coefficients[1,4],
                                          beta1=summary(model)$coefficients[2,1],stderr_beta=summary(model)$coefficients[2,2],
                                          p_b1=summary(model)$coefficients[2,4],
                                          beta2=summary(model)$coefficients[3,1],stderr_beta=summary(model)$coefficients[3,2],
                                          p_b2=summary(model)$coefficients[3,4],
                                          beta3=summary(model)$coefficients[4,1],stderr_beta=summary(model)$coefficients[4,2],
                                          p_b3=summary(model)$coefficients[4,4],
                                          adjrsq=summary(model)$adj.r.squared,ar=summary(model)$coefficients[1,1]/summary(model)$sigma*sqrt(12)))

model <- lm(firmivmgd ~ aggregateivmgd + lsiv_vw + agt_x_ivlsvw + jan, data = fs)
csts_result<-rbind(csts_result,data.frame(scaling="ivmgd_vw",alpha=summary(model)$coefficients[1,1]*12,stderr_alpha=summary(model)$coefficients[1,2]*12,
                                          p_a=summary(model)$coefficients[1,4],
                                          beta1=summary(model)$coefficients[2,1],stderr_beta=summary(model)$coefficients[2,2],
                                          p_b1=summary(model)$coefficients[2,4],
                                          beta2=summary(model)$coefficients[3,1],stderr_beta=summary(model)$coefficients[3,2],
                                          p_b2=summary(model)$coefficients[3,4],
                                          beta3=summary(model)$coefficients[4,1],stderr_beta=summary(model)$coefficients[4,2],
                                          p_b3=summary(model)$coefficients[4,4],
                                          adjrsq=summary(model)$adj.r.squared,ar=summary(model)$coefficients[1,1]/summary(model)$sigma*sqrt(12)))

model <- lm(firmmwmgd ~ aggregatemwmgd + lsmw_vw + agt_x_mwlsvw + jan, data = fs)
csts_result<-rbind(csts_result,data.frame(scaling="mwmgd_vw",alpha=summary(model)$coefficients[1,1]*12,stderr_alpha=summary(model)$coefficients[1,2]*12,
                                          p_a=summary(model)$coefficients[1,4],
                                          beta1=summary(model)$coefficients[2,1],stderr_beta=summary(model)$coefficients[2,2],
                                          p_b1=summary(model)$coefficients[2,4],
                                          beta2=summary(model)$coefficients[3,1],stderr_beta=summary(model)$coefficients[3,2],
                                          p_b2=summary(model)$coefficients[3,4],
                                          beta3=summary(model)$coefficients[4,1],stderr_beta=summary(model)$coefficients[4,2],
                                          p_b3=summary(model)$coefficients[4,4],
                                          adjrsq=summary(model)$adj.r.squared,ar=summary(model)$coefficients[1,1]/summary(model)$sigma*sqrt(12)))

model <- lm(firmglbmgd ~ aggregateglbmgd + lsglb_vw + agt_x_glblsvw + jan, data = fs)
csts_result<-rbind(csts_result,data.frame(scaling="glbmgd_vw",alpha=summary(model)$coefficients[1,1]*12,stderr_alpha=summary(model)$coefficients[1,2]*12,
                                          p_a=summary(model)$coefficients[1,4],
                                          beta1=summary(model)$coefficients[2,1],stderr_beta=summary(model)$coefficients[2,2],
                                          p_b1=summary(model)$coefficients[2,4],
                                          beta2=summary(model)$coefficients[3,1],stderr_beta=summary(model)$coefficients[3,2],
                                          p_b2=summary(model)$coefficients[3,4],
                                          beta3=summary(model)$coefficients[4,1],stderr_beta=summary(model)$coefficients[4,2],
                                          p_b3=summary(model)$coefficients[4,4],
                                          adjrsq=summary(model)$adj.r.squared,ar=summary(model)$coefficients[1,1]/summary(model)$sigma*sqrt(12)))

model <- lm(firmrvmgd ~ aggregateavgrvmgd + lsrv_vw + agt_x_rvlsvw + XR+SMB+HML+RMW+CMA+UMD + jan, data = fs)
csts_result<-rbind(csts_result,data.frame(scaling="rvmgd_ctrl_vw",alpha=summary(model)$coefficients[1,1]*12,stderr_alpha=summary(model)$coefficients[1,2]*12,
                                          p_a=summary(model)$coefficients[1,4],
                                          beta1=summary(model)$coefficients[2,1],stderr_beta=summary(model)$coefficients[2,2],
                                          p_b1=summary(model)$coefficients[2,4],
                                          beta2=summary(model)$coefficients[3,1],stderr_beta=summary(model)$coefficients[3,2],
                                          p_b2=summary(model)$coefficients[3,4],
                                          beta3=summary(model)$coefficients[4,1],stderr_beta=summary(model)$coefficients[4,2],
                                          p_b3=summary(model)$coefficients[4,4],
                                          adjrsq=summary(model)$adj.r.squared,ar=summary(model)$coefficients[1,1]/summary(model)$sigma*sqrt(12)))

model <- lm(firmivmgd ~ aggregateivmgd + lsiv_vw + agt_x_ivlsvw + XR+SMB+HML+RMW+CMA+UMD + jan, data = fs)
csts_result<-rbind(csts_result,data.frame(scaling="ivmgd_ctrl_vw",alpha=summary(model)$coefficients[1,1]*12,stderr_alpha=summary(model)$coefficients[1,2]*12,
                                          p_a=summary(model)$coefficients[1,4],
                                          beta1=summary(model)$coefficients[2,1],stderr_beta=summary(model)$coefficients[2,2],
                                          p_b1=summary(model)$coefficients[2,4],
                                          beta2=summary(model)$coefficients[3,1],stderr_beta=summary(model)$coefficients[3,2],
                                          p_b2=summary(model)$coefficients[3,4],
                                          beta3=summary(model)$coefficients[4,1],stderr_beta=summary(model)$coefficients[4,2],
                                          p_b3=summary(model)$coefficients[4,4],
                                          adjrsq=summary(model)$adj.r.squared,ar=summary(model)$coefficients[1,1]/summary(model)$sigma*sqrt(12)))

model <- lm(firmmwmgd ~ aggregatemwmgd + lsmw_vw + agt_x_mwlsvw + XR+SMB+HML+RMW+CMA+UMD + jan, data = fs)
csts_result<-rbind(csts_result,data.frame(scaling="mwmgd_ctrl_vw",alpha=summary(model)$coefficients[1,1]*12,stderr_alpha=summary(model)$coefficients[1,2]*12,
                                          p_a=summary(model)$coefficients[1,4],
                                          beta1=summary(model)$coefficients[2,1],stderr_beta=summary(model)$coefficients[2,2],
                                          p_b1=summary(model)$coefficients[2,4],
                                          beta2=summary(model)$coefficients[3,1],stderr_beta=summary(model)$coefficients[3,2],
                                          p_b2=summary(model)$coefficients[3,4],
                                          beta3=summary(model)$coefficients[4,1],stderr_beta=summary(model)$coefficients[4,2],
                                          p_b3=summary(model)$coefficients[4,4],
                                          adjrsq=summary(model)$adj.r.squared,ar=summary(model)$coefficients[1,1]/summary(model)$sigma*sqrt(12)))

model <- lm(firmglbmgd ~ aggregateglbmgd + lsglb_vw + agt_x_glblsvw + XR+SMB+HML+RMW+CMA+UMD + jan, data = fs)
csts_result<-rbind(csts_result,data.frame(scaling="glbmgd_ctrl_vw",alpha=summary(model)$coefficients[1,1]*12,stderr_alpha=summary(model)$coefficients[1,2]*12,
                                          p_a=summary(model)$coefficients[1,4],
                                          beta1=summary(model)$coefficients[2,1],stderr_beta=summary(model)$coefficients[2,2],
                                          p_b1=summary(model)$coefficients[2,4],
                                          beta2=summary(model)$coefficients[3,1],stderr_beta=summary(model)$coefficients[3,2],
                                          p_b2=summary(model)$coefficients[3,4],
                                          beta3=summary(model)$coefficients[4,1],stderr_beta=summary(model)$coefficients[4,2],
                                          p_b3=summary(model)$coefficients[4,4],
                                          adjrsq=summary(model)$adj.r.squared,ar=summary(model)$coefficients[1,1]/summary(model)$sigma*sqrt(12)))




model <- lm(firmrvmgd ~ aggregateavgrvmgd + lsrv_vw + agt_x_rvlsvw + jan, data = subset(fs,month>200706&month<200907))
csts_result<-rbind(csts_result,data.frame(scaling="rvmgd_gfc_vw",alpha=summary(model)$coefficients[1,1]*12,stderr_alpha=summary(model)$coefficients[1,2]*12,
                                          p_a=summary(model)$coefficients[1,4],
                                          beta1=summary(model)$coefficients[2,1],stderr_beta=summary(model)$coefficients[2,2],
                                          p_b1=summary(model)$coefficients[2,4],
                                          beta2=summary(model)$coefficients[3,1],stderr_beta=summary(model)$coefficients[3,2],
                                          p_b2=summary(model)$coefficients[3,4],
                                          beta3=summary(model)$coefficients[4,1],stderr_beta=summary(model)$coefficients[4,2],
                                          p_b3=summary(model)$coefficients[4,4],
                                          adjrsq=summary(model)$adj.r.squared,ar=summary(model)$coefficients[1,1]/summary(model)$sigma*sqrt(12)))

model <- lm(firmivmgd ~ aggregateivmgd + lsiv_vw + agt_x_ivlsvw + jan, data = subset(fs,month>200706&month<200907))
csts_result<-rbind(csts_result,data.frame(scaling="ivmgd_gfc_vw",alpha=summary(model)$coefficients[1,1]*12,stderr_alpha=summary(model)$coefficients[1,2]*12,
                                          p_a=summary(model)$coefficients[1,4],
                                          beta1=summary(model)$coefficients[2,1],stderr_beta=summary(model)$coefficients[2,2],
                                          p_b1=summary(model)$coefficients[2,4],
                                          beta2=summary(model)$coefficients[3,1],stderr_beta=summary(model)$coefficients[3,2],
                                          p_b2=summary(model)$coefficients[3,4],
                                          beta3=summary(model)$coefficients[4,1],stderr_beta=summary(model)$coefficients[4,2],
                                          p_b3=summary(model)$coefficients[4,4],
                                          adjrsq=summary(model)$adj.r.squared,ar=summary(model)$coefficients[1,1]/summary(model)$sigma*sqrt(12)))

model <- lm(firmmwmgd ~ aggregatemwmgd + lsmw_vw + agt_x_mwlsvw + jan, data = subset(fs,month>200706&month<200907))
csts_result<-rbind(csts_result,data.frame(scaling="mwmgd_gfc_vw",alpha=summary(model)$coefficients[1,1]*12,stderr_alpha=summary(model)$coefficients[1,2]*12,
                                          p_a=summary(model)$coefficients[1,4],
                                          beta1=summary(model)$coefficients[2,1],stderr_beta=summary(model)$coefficients[2,2],
                                          p_b1=summary(model)$coefficients[2,4],
                                          beta2=summary(model)$coefficients[3,1],stderr_beta=summary(model)$coefficients[3,2],
                                          p_b2=summary(model)$coefficients[3,4],
                                          beta3=summary(model)$coefficients[4,1],stderr_beta=summary(model)$coefficients[4,2],
                                          p_b3=summary(model)$coefficients[4,4],
                                          adjrsq=summary(model)$adj.r.squared,ar=summary(model)$coefficients[1,1]/summary(model)$sigma*sqrt(12)))

model <- lm(firmglbmgd ~ aggregateglbmgd + lsglb_vw + agt_x_glblsvw + jan, data = subset(fs,month>200706&month<200907))
csts_result<-rbind(csts_result,data.frame(scaling="glbmgd_gfc_vw",alpha=summary(model)$coefficients[1,1]*12,stderr_alpha=summary(model)$coefficients[1,2]*12,
                                          p_a=summary(model)$coefficients[1,4],
                                          beta1=summary(model)$coefficients[2,1],stderr_beta=summary(model)$coefficients[2,2],
                                          p_b1=summary(model)$coefficients[2,4],
                                          beta2=summary(model)$coefficients[3,1],stderr_beta=summary(model)$coefficients[3,2],
                                          p_b2=summary(model)$coefficients[3,4],
                                          beta3=summary(model)$coefficients[4,1],stderr_beta=summary(model)$coefficients[4,2],
                                          p_b3=summary(model)$coefficients[4,4],
                                          adjrsq=summary(model)$adj.r.squared,ar=summary(model)$coefficients[1,1]/summary(model)$sigma*sqrt(12)))

model <- lm(firmrvmgd ~ aggregateavgrvmgd + lsrv_vw + agt_x_rvlsvw + XR+SMB+HML+RMW+CMA+UMD + jan, data = subset(fs,month>200706&month<200907))
csts_result<-rbind(csts_result,data.frame(scaling="rvmgd_ctrl_gfc_vw",alpha=summary(model)$coefficients[1,1]*12,stderr_alpha=summary(model)$coefficients[1,2]*12,
                                          p_a=summary(model)$coefficients[1,4],
                                          beta1=summary(model)$coefficients[2,1],stderr_beta=summary(model)$coefficients[2,2],
                                          p_b1=summary(model)$coefficients[2,4],
                                          beta2=summary(model)$coefficients[3,1],stderr_beta=summary(model)$coefficients[3,2],
                                          p_b2=summary(model)$coefficients[3,4],
                                          beta3=summary(model)$coefficients[4,1],stderr_beta=summary(model)$coefficients[4,2],
                                          p_b3=summary(model)$coefficients[4,4],
                                          adjrsq=summary(model)$adj.r.squared,ar=summary(model)$coefficients[1,1]/summary(model)$sigma*sqrt(12)))

model <- lm(firmivmgd ~ aggregateivmgd + lsiv_vw + agt_x_ivlsvw + XR+SMB+HML+RMW+CMA+UMD + jan, data = subset(fs,month>200706&month<200907))
csts_result<-rbind(csts_result,data.frame(scaling="ivmgd_ctrl_gfc_vw",alpha=summary(model)$coefficients[1,1]*12,stderr_alpha=summary(model)$coefficients[1,2]*12,
                                          p_a=summary(model)$coefficients[1,4],
                                          beta1=summary(model)$coefficients[2,1],stderr_beta=summary(model)$coefficients[2,2],
                                          p_b1=summary(model)$coefficients[2,4],
                                          beta2=summary(model)$coefficients[3,1],stderr_beta=summary(model)$coefficients[3,2],
                                          p_b2=summary(model)$coefficients[3,4],
                                          beta3=summary(model)$coefficients[4,1],stderr_beta=summary(model)$coefficients[4,2],
                                          p_b3=summary(model)$coefficients[4,4],
                                          adjrsq=summary(model)$adj.r.squared,ar=summary(model)$coefficients[1,1]/summary(model)$sigma*sqrt(12)))

model <- lm(firmmwmgd ~ aggregatemwmgd + lsmw_vw + agt_x_mwlsvw + XR+SMB+HML+RMW+CMA+UMD + jan, data = subset(fs,month>200706&month<200907))
csts_result<-rbind(csts_result,data.frame(scaling="mwmgd_ctrl_gfc_vw",alpha=summary(model)$coefficients[1,1]*12,stderr_alpha=summary(model)$coefficients[1,2]*12,
                                          p_a=summary(model)$coefficients[1,4],
                                          beta1=summary(model)$coefficients[2,1],stderr_beta=summary(model)$coefficients[2,2],
                                          p_b1=summary(model)$coefficients[2,4],
                                          beta2=summary(model)$coefficients[3,1],stderr_beta=summary(model)$coefficients[3,2],
                                          p_b2=summary(model)$coefficients[3,4],
                                          beta3=summary(model)$coefficients[4,1],stderr_beta=summary(model)$coefficients[4,2],
                                          p_b3=summary(model)$coefficients[4,4],
                                          adjrsq=summary(model)$adj.r.squared,ar=summary(model)$coefficients[1,1]/summary(model)$sigma*sqrt(12)))

model <- lm(firmglbmgd ~ aggregateglbmgd + lsglb_vw + agt_x_glblsvw + XR+SMB+HML+RMW+CMA+UMD + jan, data = subset(fs,month>200706&month<200907))
csts_result<-rbind(csts_result,data.frame(scaling="glbmgd_ctrl_gfc_vw",alpha=summary(model)$coefficients[1,1]*12,stderr_alpha=summary(model)$coefficients[1,2]*12,
                                          p_a=summary(model)$coefficients[1,4],
                                          beta1=summary(model)$coefficients[2,1],stderr_beta=summary(model)$coefficients[2,2],
                                          p_b1=summary(model)$coefficients[2,4],
                                          beta2=summary(model)$coefficients[3,1],stderr_beta=summary(model)$coefficients[3,2],
                                          p_b2=summary(model)$coefficients[3,4],
                                          beta3=summary(model)$coefficients[4,1],stderr_beta=summary(model)$coefficients[4,2],
                                          p_b3=summary(model)$coefficients[4,4],
                                          adjrsq=summary(model)$adj.r.squared,ar=summary(model)$coefficients[1,1]/summary(model)$sigma*sqrt(12)))




#T15_double sort vol
library(sandwich)
sortinocalmar_vol<-data.frame()
for (p in unique(vol_aggregatelevel$portfolio)) {
  subset(vol_aggregatelevel,portfolio==p)
  
  sha_o<-mean(subset(vol_aggregatelevel,portfolio==p)$xret,na.rm=T)/sd(subset(vol_aggregatelevel,portfolio==p)$xret,na.rm = T)*sqrt(12)
  model<-lm(subset(vol_aggregatelevel,portfolio==p)$xret ~ 1)
  nw_se <- sqrt(NeweyWest(model, lag = 3, prewhite = FALSE)[1,1])
  t_sha_o<-mean(subset(vol_aggregatelevel,portfolio==p)$xret,na.rm=T) / nw_se
  p_sha_o <- pt(t_sha_o, df = length(subset(vol_aggregatelevel,portfolio==p)$xret) - 1, lower.tail = FALSE)
  
  sha<-mean(subset(vol_aggregatelevel,portfolio==p)$volmgd,na.rm=T)/sd(subset(vol_aggregatelevel,portfolio==p)$volmgd,na.rm = T)*sqrt(12)
  model<-lm(subset(vol_aggregatelevel,portfolio==p)$volmgd ~ 1)
  nw_se <- sqrt(NeweyWest(model, lag = 3, prewhite = FALSE)[1,1])
  t_sha<-mean(subset(vol_aggregatelevel,portfolio==p)$volmgd,na.rm=T) / nw_se
  p_sha <- pt(t_sha, df = length(subset(vol_aggregatelevel,portfolio==p)$volmgd) - 1, lower.tail = FALSE)
  
  sor_o<-mean(subset(vol_aggregatelevel,portfolio==p)$xret,na.rm=T)/sd(subset(subset(vol_aggregatelevel,portfolio==p),subset(vol_aggregatelevel,portfolio==p)$xret<0)$xret,na.rm = T)*sqrt(12)
  model<-lm(subset(subset(vol_aggregatelevel,portfolio==p),subset(vol_aggregatelevel,portfolio==p)$xret<0)$xret ~ 1)
  nw_se <- sqrt(NeweyWest(model, lag = 3, prewhite = FALSE)[1,1])
  t_sor_o<-mean(subset(vol_aggregatelevel,portfolio==p)$xret,na.rm=T) / nw_se
  p_sor_o <- pt(t_sor_o, df = length(subset(vol_aggregatelevel,portfolio==p)$xret) - 1, lower.tail = FALSE)
  
  sor<-mean(subset(vol_aggregatelevel,portfolio==p)$volmgd,na.rm=T)/sd(subset(subset(vol_aggregatelevel,portfolio==p),subset(vol_aggregatelevel,portfolio==p)$volmgd<0)$volmgd,na.rm = T)*sqrt(12)
  model<-lm(subset(subset(vol_aggregatelevel,portfolio==p),subset(vol_aggregatelevel,portfolio==p)$volmgd<0)$volmgd ~ 1)
  nw_se <- sqrt(NeweyWest(model, lag = 3, prewhite = FALSE)[1,1])
  t_sor<-mean(subset(vol_aggregatelevel,portfolio==p)$volmgd,na.rm=T) / nw_se
  p_sor <- pt(t_sor, df = length(subset(vol_aggregatelevel,portfolio==p)$volmgd) - 1, lower.tail = FALSE)
  
  total<-0
  i<-0
  yc <- numeric(26)
  for (y in unique(substr(subset(vol_aggregatelevel,portfolio==p)$month,1,4))) {
    avgcal<-sum(subset(subset(vol_aggregatelevel,portfolio==p),substr(subset(vol_aggregatelevel,portfolio==p)$month,1,4)==y)$xret,na.rm = T)/
      (max(subset(subset(vol_aggregatelevel,portfolio==p),substr(subset(vol_aggregatelevel,portfolio==p)$month,1,4)==y)$xret,na.rm = T)
       -min(subset(subset(vol_aggregatelevel,portfolio==p),substr(subset(vol_aggregatelevel,portfolio==p)$month,1,4)==y)$xret,na.rm = T))
    total<-total+avgcal
    i<-i+1
    yc[i]<-avgcal
  }
  cal_o<-total/26
  model <- lm(yc ~ 1)
  nw_se <- sqrt(NeweyWest(model, lag = 0, prewhite = FALSE)[1, 1])
  t_cal_o <- cal_o / nw_se
  p_cal_o <- pt(t_cal_o, df = 26 - 1, lower.tail = FALSE)
  
  total<-0
  i<-0
  yc <- numeric(26)
  for (y in unique(substr(subset(vol_aggregatelevel,portfolio==p)$month,1,4))) {
    avgcal<-sum(subset(subset(vol_aggregatelevel,portfolio==p),substr(subset(vol_aggregatelevel,portfolio==p)$month,1,4)==y)$volmgd,na.rm = T)/
      (max(subset(subset(vol_aggregatelevel,portfolio==p),substr(subset(vol_aggregatelevel,portfolio==p)$month,1,4)==y)$volmgd,na.rm = T)
       -min(subset(subset(vol_aggregatelevel,portfolio==p),substr(subset(vol_aggregatelevel,portfolio==p)$month,1,4)==y)$volmgd,na.rm = T))
    total<-total+avgcal
    i<-i+1
    yc[i]<-avgcal
  }
  cal<-total/26
  model <- lm(yc ~ 1)
  nw_se <- sqrt(NeweyWest(model, lag = 0, prewhite = FALSE)[1, 1])
  t_cal <- cal / nw_se
  p_cal <- pt(t_cal, df = 26 - 1, lower.tail = FALSE)
  
  sortinocalmar_vol<-rbind(sortinocalmar_vol,data.frame(scale=p,sharpe_o=sha_o,p_sharpe_o=p_sha_o, sharpe=sha,p_sharpe=p_sha, 
                                                        sortino_o=sor_o,p_sortino_o=p_sor_o,sortino=sor,p_sortino=p_sor,calmar_o=cal_o,p_calmar_o=p_cal_o,calmar=cal,p_calmar=p_cal))
}

